package userdisplays;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.Serializable;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class EditableProgressBar extends JProgressBar implements MouseListener,
        Serializable, Editable
{
    private static final long serialVersionUID = 1L;
    private static final Font font = new Font("Arial", Font.BOLD, 12); 
    private UserPanel panel;
    private boolean editable = true;

    public EditableProgressBar(UserPanel p, Color c)
    {
        panel = p;
        setFont(font);
        setMaximum(10);
        setValue(10);
        setForeground(c);
        setString("10/10");
        setStringPainted(true);
        addMouseListener(this);
    }

    public void lock()
    {
        editable = false;
    }

    public void unlock()
    {
        editable = true;
    }

    public void mouseClicked(MouseEvent arg0)
    {
        if (!editable)
            return;
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2, 2));
        p.add(new JLabel("Numerator"));
        final JTextField num = new JTextField("" + this.getValue());
        num.addFocusListener(new FocusAdapter()
        {
            public void focusGained(FocusEvent arg0)
            {
                num.selectAll();
            }
        });
        num.selectAll();
        p.add(num);
        p.add(new JLabel("Denominator"));
        final JTextField den = new JTextField("" + this.getMaximum());
        den.addFocusListener(new FocusAdapter()
        {
            public void focusGained(FocusEvent arg0)
            {
                den.selectAll();
            }
        });
        p.add(den);
        num.addAncestorListener(new AncestorListener()
        {
            public void ancestorAdded(AncestorEvent e)
            {
                JComponent component = e.getComponent();
                component.requestFocusInWindow();
                component.removeAncestorListener(this);
            }

            public void ancestorMoved(AncestorEvent e)
            {
            }

            public void ancestorRemoved(AncestorEvent e)
            {
            }
        });
        JOptionPane.showMessageDialog(null, p);
        try
        {
            int newNum = Integer.parseInt(num.getText());
            int newDen = Integer.parseInt(den.getText());
            this.setValue(newNum);
            this.setMaximum(newDen);
            this.setString(newNum + "/" + newDen);
        }
        catch (NumberFormatException e)
        {

        }
        panel.sendPanel();
    }

    public void mouseEntered(MouseEvent arg0)
    {
    }

    public void mouseExited(MouseEvent arg0)
    {
    }

    public void mousePressed(MouseEvent arg0)
    {
    }

    public void mouseReleased(MouseEvent arg0)
    {
    }
}
